#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include <QTimer>
#include <QPaintEvent>
#include <QPainter>

#include <iostream>
#include <vector>
using namespace std;

namespace Ui {
class Form;
}

#define SHOW_TIME 2000  //animation time
#define STEP_NUM 1000   //animation action number

#define MAX_SPEED 4     //start time is more quick
#define MIN_SPEED 1   //end time is  slower
#define FORMULA(x) (x*x*x)

typedef struct{
    int x,y;
    int w,h;
    int font_size;
    QWidget *subw;
}Info;

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = 0);
    ~Form();

    void paintEvent(QPaintEvent *);

    //x,y: show start point place
    //rx1,ry1: this ratio of the start point in whole place
    void init_place(int x, int y, double rx1,double ry1)
    {
        px=x,py=y;

        stepx=(-1.0*win_info.w*rx1)/STEP_NUM;
        stepy=(-1.0*win_info.h*ry1)/STEP_NUM;
        stepw=1.0*win_info.w/STEP_NUM;
        steph=1.0*win_info.h/STEP_NUM;
    }

    void add_showTimes()    //update speed
    {
        double k=MIN_SPEED-MAX_SPEED;
        k=k/FORMULA(STEP_NUM);

        double step=k*FORMULA(showTimes)+MAX_SPEED;
        showTimes+=step;
    }

    void sub_showTimes()  //update speed
    {
        double k=MIN_SPEED-MAX_SPEED;
        k=k/FORMULA(STEP_NUM);

        double step=k*FORMULA(showTimes)+MAX_SPEED;
        showTimes-=step;
    }

    void setInfo(QWidget *w, Info &info)    //initial information
    {
        info.x=w->x();
        info.y=w->y();
        info.w=w->width();
        info.h=w->height();
        info.font_size=w->font().pointSize();
    }

    void addInfo(QWidget *w)    //initial information
    {
        Info info;
        info.subw=w;
        info.x=w->x();
        info.y=w->y();
        info.w=w->width();
        info.h=w->height();
        info.font_size=w->font().pointSize();
        parts.push_back(info);
    }

    void update_parts(double ratio);    //update sub widgets

    void update_part(QWidget *w, double ratio, Info info); //update sub widget

    void start_show();  //show

    void close_show();  //close


private slots:
    void expand_window();

    void narrow_window();

    void on_pushButton_clicked();

private:
    Ui::Form *ui;

    //window parameters
    int px,py;
    //animation parameters
    double stepx;
    double stepy;
    double stepw;
    double steph;

    //animation showed action number
    double showTimes;

    //window information
    Info win_info;

    //sub widgets information
    vector<Info> parts;

    //timers
    QTimer *start_timer;
    QTimer *close_timer;
};

#endif // FORM_H
